//var host = '';
//const appUrl = 'http://www.ydz.com';
const appUrl = 'https://yc.163100.com';
//const appUrl = 'https://m.youdianzi.net';
let requestObj; // 存储上一次的请求对象
function request(data) {
  var temp = data.data;
  let token;
  try {
    var value = wx.getStorageSync('token')
    if (value) {
      token=value
      // Do something with return value
    }
  } catch (e) {
    // Do something when catch error
  }
  // Object.assign({}, { token: token }, temp)
  if(temp.abort && requestObj) {
    requestObj.abort();
  }
  if (token) {
    return new Promise(function (resolve, reject) {
      const currentRequest = wx.request({
        data: Object.assign({}, { token: token }, temp),
        url: appUrl + data.url,
        header: {
          "content-type": "application/json;charset=UTF-8"
        },
        method: data.method,
        success: res => {
          resolve(res.data)
        },
        fail: function (res) {
          reject(res)
        },
        complete: function (res) {
          // complete
          wx.hideNavigationBarLoading() //完成停止加载
          //wx.stopPullDownRefresh() //停止下拉刷新
        }
      })
      requestObj = temp.abort ? currentRequest: null;
    });
  } else if (temp.code == 110) {
    wx.showToast({
      title: '登陆失效从新登陆',
      icon: 'warn',
      duration: 2000
    })
  } else {
    return new Promise(function (resolve, reject) {
      wx.request({
        data: temp,
        url: appUrl + data.url,
        header: {
          "content-type": "application/json;charset=UTF-8"
        },
        method: data.method,
        success: res => {
          resolve(res.data)
        },
        fail: function (res) {
          reject(res)
        },
        complete: function (res) {
          // complete
          wx.hideNavigationBarLoading() //完成停止加载
          //wx.stopPullDownRefresh() //停止下拉刷新
        }
      })
    });
  }

};

//module.exports.request = request
export default request
